package org.uim.cricket.storage.raise.fetch;

import java.io.File;

import org.uim.cricket.entities.match.SeriesPlayed;
import org.uim.cricket.storage.StorageProperties;

public class SeriesArgumentBuilder implements FetchArgumentBuildable {

	private int status = 0;
	
	@Override
	public String buildArgument(Object obj) throws Exception {
		SeriesPlayed s = (SeriesPlayed) obj;
		
		if (status % 2 == 0) {
			status++;
			
			String dir = StorageProperties.getValue("repo.fs.series.dir");
			File f = new File(dir);
			if (dir == null || !f.exists()) {
				throw new IllegalArgumentException (" Can't find a FileSystem resource location for series!");
			} else if (!f.isDirectory()) {
				throw new IllegalArgumentException (" Resource location is not a directory!");
			}
			
			if (!dir.endsWith("/")) {
				dir += "/";
			}
			
			if (s.getId() == null) {
				throw new IllegalArgumentException (" Series id can't be null!");
			} else {
				return dir + s.getId() + ".html";
			}
			
		} else {
			
			String url = StorageProperties.getValue("repo.web.series.url");
			if (url == null) {
				throw new IllegalArgumentException (" Can't find a URL resource location for series!");
			}
			if (!url.endsWith("/")) url += "/";
			
			return url + s.getId() + ".html";
		}
	}

}
